<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属小区" prop="communityId">
        <el-select v-model="queryParams.communityId"   placeholder="请选择所属小区" filterable clearable size="small" @change="getUnitListByCommunityId(queryParams.communityId)">
          <el-option
            v-for="community in communityOptions"
            :key="community.id"
            :label="community.name"
            :value="Number(community.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单元名称" prop="unitId">
        <el-select v-model="queryParams.unitId"   placeholder="请选择单元名称" filterable clearable size="small" >
          <el-option
            v-for="unit in unitOptions"
            :key="unit.id"
            :label="unit.unitName"
            :value="Number(unit.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门禁名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入门禁名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceNo">
        <el-input
          v-model="queryParams.deviceNo"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:communityDoor:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:communityDoor:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:communityDoor:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:communityDoor:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="communityDoorList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="小区门id" align="center" prop="id" v-if="false"/>
      <el-table-column label="小区名称" align="center" prop="communityName" />
      <el-table-column label="单元名称" align="center" prop="unitName" />
      <el-table-column label="门禁名称" align="center" prop="deviceName" />
      <el-table-column label="设备验证码" align="center" prop="deviceId" />
      <el-table-column label="设备类型" align="center" prop="deviceType" :formatter="deviceTypeFormat" />
      <el-table-column label="设备ip" align="center" prop="deviceIp" />
      <el-table-column label="设备端口号" align="center" prop="port" />
      <el-table-column label="设备用户名" align="center" prop="userName" />
      <el-table-column label="设备密码" align="center" prop="password" />
      <el-table-column label="设备编号" align="center" prop="deviceNo" />
      <el-table-column label="权限类型" align="center" prop="powerType" :formatter="powerTypeFormat" />
      <el-table-column label="在线状态" align="center" prop="onLine" :formatter="onLineFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:communityDoor:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:communityDoor:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门禁状态管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true"  label-width="120px">
        <el-form-item label="所属小区" prop="communityId">
          <el-select v-model="form.communityId"   placeholder="请选择所属小区" filterable clearable size="small" @change="getUnitListByCommunityId(form.communityId)">
            <el-option
              v-for="community in communityOptions"
              :key="community.id"
              :label="community.name"
              :value="Number(community.id)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属单元" prop="unitId">
          <el-select v-model="form.unitId"   placeholder="请选择单元名称" filterable clearable size="small" >
            <el-option
              v-for="unit in unitOptions"
              :key="unit.id"
              :label="unit.unitName"
              :value="Number(unit.id)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="绑定摄像头" prop="cameraId">
          <el-select v-model="form.cameraId"   placeholder="请绑定摄像头" filterable clearable size="small" >
            <el-option
              v-for="camera in cameraOptions"
              :key="camera.id"
              :label="camera.name"
              :value="Number(camera.id)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="门禁名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入门禁名称" />
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in deviceTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限类型" prop="powerType">
          <el-select v-model="form.powerType" placeholder="请选择权限类型">
            <el-option
              v-for="dict in powerTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备验证码" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备验证码" />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceNo">
          <el-input v-model="form.deviceNo" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备ip" prop="deviceIp">
          <el-input v-model="form.deviceIp" placeholder="请输入设备ip" />
        </el-form-item>
        <el-form-item label="设备用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入设备用户名" />
        </el-form-item>
        <el-form-item label="设备密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入设备密码" />
        </el-form-item>
        <el-form-item label="设备端口号" prop="port">
          <el-input v-model="form.port" placeholder="请输入设备端口号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommunityDoor, getCommunityDoor, delCommunityDoor, addCommunityDoor, updateCommunityDoor, exportCommunityDoor } from "@/api/system/communityDoor";
import {getAllList} from "@/api/system/community";
import {getListByCommunity} from "@/api/system/communityUnit";
import {getCameraList} from "@/api/system/camera";
export default {
  name: "CommunityDoor",
  components: {
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门禁状态管理表格数据
      communityDoorList: [],
      //小区下拉列表
      communityOptions: [],
      //小区单元下拉列表
      unitOptions: [],
      //摄像头下拉列表
      cameraOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 设备类型：1趣开门，2海康字典
      deviceTypeOptions: [],
      // 权限类型：1单元门,2公共门字典
      powerTypeOptions: [],
      // 设备在线状态：1在线  0离线 3损坏字典
      onLineOptions: [],
      // 设备激活状态：1已激活  0未激活字典
      deviceStatusOptions: [],
      // 广告类型：1图片，2视频字典
      bannerTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        communityId: undefined,
        unitId: undefined,
        deviceNo: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCommunityList();
    this.getCameraList();
    this.getDicts("community_door_device_type").then(response => {
      this.deviceTypeOptions = response.data;
    });
    this.getDicts("community_door_power_type").then(response => {
      this.powerTypeOptions = response.data;
    });
    this.getDicts("community_door_on_line").then(response => {
      this.onLineOptions = response.data;
    });
    this.getDicts("community_door_device_status").then(response => {
      this.deviceStatusOptions = response.data;
    });
    this.getDicts("community_door_banner_type").then(response => {
      this.bannerTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询门禁状态管理列表 */
    getList() {
      this.loading = true;
      listCommunityDoor(this.queryParams).then(response => {
        this.communityDoorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取摄像头下拉列表
    getCameraList(){
      getCameraList().then(response => {
        this.cameraOptions = response.data
      });
    },
    //获取小区列表
    getCommunityList(){
      let data={};
      getAllList(data).then(response => {
        this.communityOptions = response.data
      });
    },
    //获取小区单元列表
    getUnitListByCommunityId(communityId){
      this.unitOptions = [];
      this.queryParams.unitId = undefined;
      this.form.unitId = undefined;
      let data={
        communityId:communityId
      };
      getListByCommunity(data).then(response => {
        this.unitOptions = response.data
      });
    },
    // 设备类型：1趣开门，2海康字典翻译
    deviceTypeFormat(row, column) {
      return this.selectDictLabel(this.deviceTypeOptions, row.deviceType);
    },
    // 权限类型：1单元门,2公共门字典翻译
    powerTypeFormat(row, column) {
      return this.selectDictLabel(this.powerTypeOptions, row.powerType);
    },
    // 设备在线状态：1在线  0离线 3损坏字典翻译
    onLineFormat(row, column) {
      return this.selectDictLabel(this.onLineOptions, row.onLine);
    },
    // 设备激活状态：1已激活  0未激活字典翻译
    deviceStatusFormat(row, column) {
      return this.selectDictLabel(this.deviceStatusOptions, row.deviceStatus);
    },
    // 广告类型：1图片，2视频字典翻译
    bannerTypeFormat(row, column) {
      return this.selectDictLabel(this.bannerTypeOptions, row.bannerType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deviceName: undefined,
        deviceId: undefined,
        deviceType: undefined,
        deviceIp: undefined,
        port: undefined,
        userName: undefined,
        password: undefined,
        deviceNo: undefined,
        powerType: undefined,
        onLine: undefined,
        deviceStatus: undefined,
        appVersion: undefined,
        model: undefined,
        cameraId: undefined,
        unitId: undefined,
        communityId: undefined,
        bannerType: undefined,
        bannerDuration: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加门禁状态管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getCommunityDoor(id).then(response => {
        this.loading = false;
        this.getUnitListByCommunityId(response.data.communityId);
        this.form = response.data;
        this.open = true;
        this.title = "修改门禁状态管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCommunityDoor(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommunityDoor(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除门禁状态管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delCommunityDoor(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有门禁状态管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCommunityDoor(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
